package com.leather.question.receiver;

import com.leather.common.toolkits.BeanKit;
import com.leather.common.vo.MessageVo;
import com.leather.question.config.MQConfig;
import com.leather.question.feignclient.MessageClient;
import com.leather.question.mapper.AnswerMapper;
import com.leather.question.mapper.QuestionMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class QuestionQueneReceiver {

    @Autowired
    private MessageClient messageClient;

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @RabbitListener(queues = MQConfig.MESSAGE_QUENE)
    public void receive(String message) {
        MessageVo messageVo = BeanKit.stringToBean(message, MessageVo.class);
        messageClient.sendMessage(messageVo);
    }

    @RabbitListener(queues = MQConfig.DELETE_ANSWER_QUENE)
    public void answerDelete(String message) {
        int questionId = Integer.parseInt(message);
        answerMapper.setFlag(questionId);
    }

    @RabbitListener(queues = MQConfig.ANSWERS_QUEUE)
    public void answerOption(String message) {
        String[] str = message.split("-");
        int type = Integer.parseInt(str[0]);
        int questionId = Integer.parseInt(str[1]);
        if (type == 1) {
            questionMapper.answerAdd(questionId);
        } else {
            questionMapper.answerRemove(questionId);
        }
    }


}
